package minesweeper;

import javax.swing.JLabel;

import java.awt.Color;
import java.util.TimerTask;

public class TimerDisplay extends JLabel
{
    private final static long STEPS = 1000; 
    private int seconds=-1;
    private int minutes=0;
    private java.util.Timer timer;
    private boolean started;
    public TimerDisplay() {
        setForeground(Color.red);
        setHorizontalAlignment(JLabel.CENTER);
        setText(""+0);
        started = false;
    }

    public void start() {
        started = true;
        timer=new java.util.Timer();
            TimerTask task=new TimerTask() {
                public void run() {
                    if(minutes > 60){
                        setText("Seriously?");
                    } else {
                        if(seconds > 60){
                            minutes++;
                            seconds = 0;
                        } else {
                            seconds++;                       
                        }
                        if(seconds > 10){
                            setText(String.valueOf(minutes) + ":" + String.valueOf(seconds));  
                        } else {
                            setText(String.valueOf(minutes) + ":" + "0" + String.valueOf(seconds));
                        }
                                          
                        //setText(""+nbSeconds);                      
                    }

                }
            };
        timer.scheduleAtFixedRate(task,0,STEPS);
    }
  
    public void stop() {
        timer.cancel();
        started = false;
    }
  
    public void reset() {
        timer.cancel();
        seconds=-1;
        setText(""+0);    
    }
    
    public void startAt(int minutes,int seconds){
        this.seconds = seconds;
        this.minutes = minutes;
        start();
        
    }
    public boolean isStarted(){ return started;} 
  
    public int getMinutes() {return minutes;}
    public int getSeconds() {return seconds;}

/*   public static void main(String args[]) {
        JFrame f =new JFrame();
        JPanel j = new JPanel();
        TimerDisplay timer=new TimerDisplay();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        j.add(timer);
        f.add(j);
        f.pack();
        f.setVisible(true);
        timer.start();
    }*/
}
